//
//  VerticalCenterTextFieldCell.swift
//  MyColorPicker
//
//  Created by 火星人 on 2018/9/21.
//  Copyright © 2018年 火星人. All rights reserved.
//

import Cocoa

class VerticalCenterTextFieldCell: NSTextFieldCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.controlView?.wantsLayer = true
        self.controlView?.layer?.backgroundColor = CGColor.white
    }
    
    func newFrame(_ cellFrame: NSRect) -> NSRect {
        let fh = self.attributedStringValue.size().height
        let offset = (cellFrame.height - fh) / 2
        let left = CGFloat(2)
        return CGRect(x: left, y: offset, width: cellFrame.width - left - left, height: cellFrame.height - offset - offset)
    }
    
    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        super.drawInterior(withFrame: self.newFrame(cellFrame), in: controlView)
    }
    
    override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
        super.edit(withFrame: self.newFrame(rect), in: controlView, editor: textObj, delegate: delegate, event: event)
    }
    
    override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
        super.select(withFrame: self.newFrame(rect), in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
    }
}
